﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Xml.Linq;
namespace WPFExcelReport
{
    public partial class YCAAUTOAB2INTEX : BaseFunction
    {
        public DataSet SetData = new DataSet();
        public DataSet SetDispo = new DataSet();
        public DateTime tomorrow = DateTime.Now.AddDays(1);
        public DateTime yesterday = DateTime.Now.AddDays(-1);
        public string ITXMANDANT = "00", ITXDBVERSION = "50.24", ITXUSER = "ITXVVVK ", ITXDATE = "20120723", ITXVKKRS = "C0", ITXVKLFD = "100227";
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public string today = "";
        public string Right(string str, int num)
        {
            try
            {
                int leng = str.Length;
                str = str.Substring(leng - num, num);
            }
            catch (Exception ex)
            {
            }
            return str;
        }
        public string Left(string str, int num)
        {
            try
            {
                int leng = str.Length;
                str = str + "                                                                                ";
                str = str.Substring(0, num);
            }
            catch (Exception ex)
            {

            }
            return str;
        }
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }

        public YCAAUTOAB2INTEX( )
            : base()
        {
             
        }
        /// <summary>
        /// 通过客户0实现不同的组合剪板条件设定。
        /// </summary>
        /// <param name="xdoc"></param>
        public void Execute(XDocument xdoc = null)
        {

            string KRS="", LFD="", POS=""; 
            #region==ABPOS====

            if (SetData.Tables.Contains("ABPOS"))
            {
                foreach (DataRow row in SetData.Tables["ABPOS"].Rows)
                {
                    KRS = row["ABPAUFKRS"].ToString();
                    LFD = row["ABPAUFLFD"].ToString();
                    POS = row["ABPAUFPOS"].ToString();
                    EventDispatcher.Instance.RaiseStatus("开始保存ABPOS:" + KRS + "_" +LFD+ "_" +POS , StatusEventType.RunningProgress);
                    INTEXDBC.SaveABPOS(row);
                    INTEXDBC.DeleteICC_ABPOS_EXT(row, 2);
                }
            }
            #endregion
            #region===ABAUFM===ok

            if (SetData.Tables.Contains("ABAUFM"))
            {
                foreach (DataRow row in SetData.Tables["ABAUFM"].Rows)
                {
                    EventDispatcher.Instance.RaiseStatus("开始保存ABAUFM:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                    INTEXDBC.SaveABAUFM(row);
                }
            }
            #endregion
            #region===ABFOLGE===ok
            if (SetData.Tables.Contains("ABFOLGE"))//ok
            {
                EventDispatcher.Instance.RaiseStatus("开始保存ABFOLGE:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["ABFOLGE"].Rows)
                {
                    INTEXDBC.SaveABFOLGE(row);
                }
            }

            #endregion
            #region=========KSLEIST================ok
            if (SetData.Tables.Contains("KSLEIST"))//ok
            {
                EventDispatcher.Instance.RaiseStatus("开始保存KSLEIST:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["KSLEIST"].Rows)
                {
                    INTEXDBC.SaveKSLEIST(row);
                }
            }

            #endregion
            #region=========WBBST================OK
            if (SetData.Tables.Contains("WBBST"))
            {
                EventDispatcher.Instance.RaiseStatus("开始保存WBBST:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["WBBST"].Rows)
                {
                    INTEXDBC.SaveWBBST(row);
                }
            }
            #endregion
            #region==WGBST====OK
            if (SetData.Tables.Contains("WGBST"))
            {
                EventDispatcher.Instance.RaiseStatus("开始保存WGBST:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["WGBST"].Rows)
                {
                    INTEXDBC.SaveWGBST(row);
                }
            }
            #endregion
            #region===WBDAT=== OK
            if (SetData.Tables.Contains("WBDAT"))
            {
                EventDispatcher.Instance.RaiseStatus("开始保存WBDAT:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["WBDAT"].Rows)
                {
                    INTEXDBC.SaveWBDAT(row);
                }
            }
            #endregion
            #region=WBBEW=====ok

            if (SetData.Tables.Contains("WBBEW"))
            {
                EventDispatcher.Instance.RaiseStatus("开始保存WBBEW:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["WBBEW"].Rows)
                {
                    INTEXDBC.SaveWBBEW(row);
                }
            }
            #endregion
            #region===WDECK===ok

            if (SetData.Tables.Contains("WDECK"))//ok
            {
                EventDispatcher.Instance.RaiseStatus("开始保存WDECK:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["WDECK"].Rows)
                {
                    INTEXDBC.SaveWDECK(row);
                }
            }
            #endregion
            #region=========FDECK================ok
            if (SetData.Tables.Contains("FDECK"))//ok
            {
                EventDispatcher.Instance.RaiseStatus("开始保存FDECK:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["FDECK"].Rows)
                {
                    INTEXDBC.SaveFDECK(row);
                }
            }
            #endregion
            #region===FBST===OK
            if (SetData.Tables.Contains("FBST"))
            {
                EventDispatcher.Instance.RaiseStatus("开始保存FBST:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["FBST"].Rows)
                {
                    if (row["FBME"].ToString().Trim() == "0")
                    {
                        INTEXDBC.DeleteFBST(row);
                    }
                    else
                    {
                        INTEXDBC.SaveFBST(row);
                    }
                }
            }
            #endregion
            #region==FGBST====Hope

            if (SetData.Tables.Contains("FGBST"))
            {
                EventDispatcher.Instance.RaiseStatus("开始保存FGBST:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["FGBST"].Rows)
                {
                    INTEXDBC.SaveFGBST(row);
                }
            }
            #endregion
            #region==FBEW====    ok
            if (SetData.Tables.Contains("FBEW"))//ok
            {
                EventDispatcher.Instance.RaiseStatus("开始保存FBEW:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["FBEW"].Rows)
                {
                    INTEXDBC.SaveFBEW(row);
                }
            }
            #endregion
            #region===FCHAR===ok

            if (SetData.Tables.Contains("FCHAR"))//ok
            {
                EventDispatcher.Instance.RaiseStatus("开始保存FCHAR:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["FCHAR"].Rows)
                {
                    INTEXDBC.SaveFCHAR(row);
                }
            }
            #endregion
            
            DataRow dwData = INTEXDB.ICC_ABPOS_MAT.NewRow();
            dwData["KRS"] = KRS;
            dwData["LFD"] = LFD;
            INTEXDBC.UpdateICC_ABPOS_MAT(dwData, 31);
            INTEXDBC.UpdateICC_ABPOS_MAT(dwData, 32);
            INTEXDBC.UpdateICC_ABPOS_MAT(dwData, 33);
            INTEXDBC.UpdateICC_ABPOS_MAT(dwData, 34);
            INTEXDBC.UpdateICC_ABPOS_MAT(dwData, 35);
            INTEXDBC.UpdateICC_ABPOS_MAT(dwData, 36);
            INTEXDBC.UpdateICC_ABPOS_MAT(dwData, 37);
            INTEXDBC.UpdateICC_ABPOS_MAT(dwData, 38);
            
            EventDispatcher.Instance.RaiseStatus("保存完成", StatusEventType.StopProgress);
        }
        public void ReSet(DataSet setData, DataSet setDispo)
        {
            try
            {
                this.SetData = setData;
                this.SetDispo = setDispo;
                UserID = "INTEX   ";
                if (!this.SetData.Tables.Contains("INTEXMAP"))
                    return;
                if (this.SetData.Tables["INTEXMAP"].Rows.Count == 0)
                    return;
                DataRow dwIntex = this.SetData.Tables["INTEXMAP"].Rows[0];
                ITXMANDANT = dwIntex["ITXMANDANT"].ToString();
                ITXVKKRS = dwIntex["ITXVKKRS"].ToString();
                ITXVKLFD = dwIntex["ITXVKLFD"].ToString();
                ITXUSER = dwIntex["ITXUSER"].ToString();
                ITXDATE = dwIntex["ITXDATE"].ToString();
                ITXDBVERSION = dwIntex["ITXDBVERSION"].ToString();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void Delete(string client, string krs, string lfd, string pos = "000")
        {
            try
            {   
                DataSet setResult = INTEXDBC.QueryEKPOS(client, krs, lfd);
                if (!setResult.Tables.Contains("EKPOS"))
                    return;
                int count = setResult.Tables["EKPOS"].Rows.Count;
                int cdel = 0;

                var query = from DataRow row in setResult.Tables["EKPOS"].AsEnumerable()
                            select row;
                if (pos == "000")
                {
                    foreach (DataRow row in query)
                    {
                        decimal EKPME3 = System.Convert.ToDecimal(row["EKPME3"].ToString());
                        string EKPBEKZ = row["EKPBEKZ"].ToString();
                        string EKPKNTRNRL = row["EKPKNTRNRL"].ToString();
                        if (EKPBEKZ.Trim() != "")
                        {
                            MessageBox.Show("采购单数量已经确认，不能删除!");
                            return;
                        }
                        if (EKPKNTRNRL.Trim() != "")
                        {
                            MessageBox.Show("采购单已经审核，不能删除!");
                            return;
                        }
                        if (EKPME3 > 0)
                        {
                            MessageBox.Show("该采购单已经入库，不能删除!");
                            return;
                        }
                    }
                }
                if (pos != "000")
                {
                    query = from DataRow row in setResult.Tables["EKPOS"].AsEnumerable()
                            where row["EKPAUFPOS"].ToString() == pos
                            select row;
                    foreach (DataRow row in query)
                    {
                        decimal EKPME3 = System.Convert.ToDecimal(row["EKPME3"].ToString());

                        if (EKPME3 > 0)
                        {
                            if (MessageBox.Show("需要继续单行删除，请确认", "该采购单已经入库，不能删除!", MessageBoxButton.YesNo) == MessageBoxResult.No)
                            {
                                return;
                            }
                        }
                    }
                }
                foreach (DataRow row in setResult.Tables["EKPOS"].Rows)
                {

                    string posh = row["EKPAUFPOS"].ToString();
                    if (pos != "000")
                    {
                        if (posh != pos)
                            continue;
                    }
                    decimal EKPME3 = System.Convert.ToDecimal(row["EKPME3"].ToString());
                    if (EKPME3 > 0)
                    {
                        MessageBox.Show("该采购单已经入库，不能删除!");
                        continue;
                    }
                    INTEXDBC.DeleteEKPOS(client, krs, lfd, posh);
                    cdel++;
                    INTEXDBC.UpdateICC_ABAUFM("EK", client, krs, lfd, posh);
                }
                if (cdel == count)
                {
                    INTEXDBC.DeleteEKKOPF(client, krs, lfd);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}